<template>
  <div class="about">
    <div v-for="(item,index) in 90" :key="index">fdafdsfdasfd</div>
    <img
      data-src="https://img0.baidu.com/it/u=1929941019,3324507395&fm=253&fmt=auto&app=120&f=JPEG?w=889&h=500"
      src="https://img2.baidu.com/it/u=3676725520,1526302790&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"
      alt=""
    />
    <img
      data-src="https://img0.baidu.com/it/u=3613575871,1099775454&fm=253&fmt=auto&app=120&f=JPEG?w=1280&h=800"
      src="https://img2.baidu.com/it/u=3676725520,1526302790&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"
      alt=""
    />
    <img
      data-src="https://img0.baidu.com/it/u=115900785,3349192519&fm=253&fmt=auto&app=120&f=JPEG?w=889&h=500"
      src="https://img2.baidu.com/it/u=3676725520,1526302790&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"
      alt=""
    />
    <img
      data-src="https://img0.baidu.com/it/u=4004455835,1312089709&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500"
      src="https://img2.baidu.com/it/u=3676725520,1526302790&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"
      alt=""
    />
    <img
      data-src="https://img2.baidu.com/it/u=4045433179,2457917507&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=281"
      src="https://img2.baidu.com/it/u=3676725520,1526302790&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"
      alt=""
    />
    <img
      data-src="https://img2.baidu.com/it/u=678843919,2490464996&fm=253&fmt=auto&app=120&f=JPEG?w=1280&h=800"
      src="https://img2.baidu.com/it/u=3676725520,1526302790&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"
      alt=""
    />
    <img
      data-src="https://img1.baidu.com/it/u=294609478,355535538&fm=253&fmt=auto&app=120&f=JPEG?w=1422&h=800"
      src="https://img2.baidu.com/it/u=3676725520,1526302790&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"
      alt=""
    />
    <img
      data-src="https://img2.baidu.com/it/u=505291809,262986200&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500"
      src="https://img2.baidu.com/it/u=3676725520,1526302790&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"
      alt=""
    />
    <img
      data-src="https://img1.baidu.com/it/u=3128798340,624925585&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500"
      src="https://img2.baidu.com/it/u=3676725520,1526302790&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"
      alt=""
    />
    <img
      data-src="https://img1.baidu.com/it/u=1156098146,1096029407&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=281"
      src="https://img2.baidu.com/it/u=3676725520,1526302790&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"
      alt=""
    />
    <img
      data-src="https://img0.baidu.com/it/u=1865592685,4088896322&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500"
      src="https://img2.baidu.com/it/u=3676725520,1526302790&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"
      alt=""
    />
    <img
      data-src="https://img0.baidu.com/it/u=3993901385,893039060&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500"
      src="https://img2.baidu.com/it/u=3676725520,1526302790&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"
      alt=""
    />
    <img
      data-src="https://img1.baidu.com/it/u=2339405713,2211417036&fm=253&fmt=auto&app=120&f=JPEG?w=800&h=500"
      src="https://img2.baidu.com/it/u=3676725520,1526302790&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"
      alt=""
    />
    <img
      data-src="https://img1.baidu.com/it/u=753524162,2169708616&fm=253&fmt=auto&app=120&f=JPEG?w=1422&h=800"
      src="https://img2.baidu.com/it/u=3676725520,1526302790&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"
      alt=""
    />
    <img
      data-src="https://img2.baidu.com/it/u=2919717010,538569205&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=313"
      src="https://img2.baidu.com/it/u=3676725520,1526302790&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"
      alt=""
    />
    <img
      data-src="https://img1.baidu.com/it/u=3530895056,1635635486&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500"
      src="https://img2.baidu.com/it/u=3676725520,1526302790&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"
      alt=""
    />
    <img
      data-src="https://img0.baidu.com/it/u=2418253345,3234045140&fm=253&fmt=auto&app=120&f=JPEG?w=1280&h=800"
      src="https://img2.baidu.com/it/u=3676725520,1526302790&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"
      alt=""
    />
    <img
      data-src="https://img2.baidu.com/it/u=1739498176,3176536548&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=313"
      src="https://img2.baidu.com/it/u=3676725520,1526302790&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"
      alt=""
    />
    <img
      data-src="https://img1.baidu.com/it/u=3303860160,3225259321&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500"
      src="https://img2.baidu.com/it/u=3676725520,1526302790&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"
      alt=""
    />
  </div>
</template>
<script>
export default {
  data() {
    return {};
  },
  mounted() {
    // onload是等所有的资源文件加载完毕以后再绑定事件
    window.onload = function () {
      // 获取图片列表，即img标签列表
      var imgs = document.querySelectorAll("img");
      console.log(imgs)
      // 获取到浏览器顶部的距离
      function getTop(e) {
        return e.offsetTop;
      }
      // 懒加载实现
      function lazyload(imgs) {
        // 可视区域高度
        var h = window.innerHeight;
        //滚动区域高度
        var s = document.documentElement.scrollTop || document.body.scrollTop;
        for (var i = 0; i < imgs.length; i++) {
          //图片距离顶部的距离大于可视区域和滚动区域之和时懒加载
          if (h + s > getTop(imgs[i])) {
            // 真实情况是页面开始有2秒空白，所以使用setTimeout定时2s
            (function (i) {
              setTimeout(function () {
                // 不加立即执行函数i会等于9
                // 隐形加载图片或其他资源，
                //创建一个临时图片，这个图片在内存中不会到页面上去。实现隐形加载
                var temp = new Image();
                temp.src = imgs[i].getAttribute("data-src"); //只会请求一次
                // onload判断图片加载完毕，真是图片加载完毕，再赋值给dom节点
                temp.onload = function () {
                  // 获取自定义属性data-src，用真图片替换假图片
                  imgs[i].src = imgs[i].getAttribute("data-src");
                };
              }, 2000);
            })(i);
          }
        }
      }
      lazyload(imgs);

      // 滚屏函数
      window.onscroll = function () {
        lazyload(imgs);
      };
    };
  },
};
</script>
<style lang="scss">
.about {
  img {
    width: 400px;
    height: 380px;
    display: block;
  }
}
</style>
